import { formatSeconds } from '@/utils/index'
// 倒计时
export default function useTimeTask() {
  const timeText = ref<string>('00:00')
  const remainTime = ref<number>(0)
  const timer = ref<null|NodeJS.Timeout>();
  // 开始倒计时
  function startCountDown(times: number, finish?:()=>void) {
    remainTime.value = times
    timeText.value = formatSeconds(remainTime.value, 'mm:ss')
    timer.value = setInterval(() => {
      if(remainTime.value>0) {
        remainTime.value--;
        timeText.value = formatSeconds(remainTime.value, 'mm:ss')
      } else {
        if(timer.value) {
          clearInterval(timer.value)
          timer.value = null
        }
        if(finish) {
          finish()
        }
      }
    }, 1000)
  }
  return { startCountDown, timeText, remainTime }
}

